/*
 * @Author       : Bing Chen chenbing@iocharger.com
 * @Date         : 2024-05-20 16:52:01
 * @LastEditors  : Bing Chen chenbing@iocharger.com
 * @LastEditTime : 2025-03-15 12:52:38
 * @FilePath     : \openchaoji-corelib\secc\include\OpenChaoJi_secc_app.h
 * @Description  : 本文件定义OpenChaoJi库提供给secc的app调用的接口
 *
 * Copyright (c) 2025 OpenChaoJi
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the icense for the specific language governing permissions and
 * limitations under the License.
 *
 */

#ifndef __OPEN_CHAOJI_SECC_APP_H__
#define __OPEN_CHAOJI_SECC_APP_H__

#include "../../shared/include/OpenChaoJi_shared.h"

struct OpenChaoJi_secc_context;

struct OpenChaoJi_secc_app_start_parameter
{
#if (OPENCHAOJI_CONFIG_ENABLE_GBT27930_B == CONFIG_YES)
    // 功能协商
    uint8_t fdc_support_auth[8];         // enum SystemB_FDCSupportType 充电机对鉴权功能模块FDC的支持情况
    uint8_t fdc_support_reserve[8];      // enum SystemB_FDCSupportType 充电机对预约功能模块FDC的支持情况
    uint8_t fdc_support_selfcheck[8];    // enum SystemB_FDCSupportType 充电机对系统自检功能模块FDC的支持情况
    uint8_t fdc_support_supply[8];       // enum SystemB_FDCSupportType 充电机对供电模式功能模块FDC的支持情况
    uint8_t fdc_support_energy[8];       // enum SystemB_FDCSupportType 充电机对预充及能量传输功能模块FDC的支持情况
    uint8_t fdc_support_statistics[8];   // enum SystemB_FDCSupportType 充电机对服务统计功能模块FDC的支持情况
    uint8_t fdc_support_service_idle[8]; // enum SystemB_FDCSupportType 充电机对服务空闲功能模块FDC的支持情况
#endif

    // 参数配置
    uint16_t parameter_max_charging_volt;  // SystemB_VoltageType1 充电机最高充电电压
    uint16_t parameter_min_charging_volt;  // SystemB_VoltageType1 充电机最低充电电压
    uint16_t parameter_max_charging_curr;  // SystemB_CurrentType  充电机最大充电电流
    uint16_t parameter_min_charging_curr;  // SystemB_CurrentType  充电机最小充电电流
    uint16_t parameter_max_discharge_volt; // SystemB_VoltageType1 充电机最高放电电压
    uint16_t parameter_min_discharge_volt; // SystemB_VoltageType1 充电机最低放电电压
    uint16_t parameter_max_discharge_curr; // SystemB_CurrentType  充电机最高放电电流
    uint16_t parameter_min_discharge_curr; // SystemB_CurrentType  充电机最低放电电流
    uint16_t parameter_raise_rate_curr;    // SystemB_ChargingRateType 充电机电流上调最小速率
    uint16_t parameter_reduce_rate_curr;   // SystemB_ChargingRateType 充电机电流下调最小速率

#if (OPENCHAOJI_CONFIG_ENABLE_GBT27930_B == CONFIG_YES)
    // 鉴权
    uint16_t rfid_auth_m_time;    //rfid 鉴权超时最大时间 单位分钟
#endif
};

// lib需要App确认当前的授权状态, 返回0 - 等待授权中, 返回1 - 授权成功, 返回-1 - 授权失败
enum secc_app_auth_status
{
    SECC_APP_AUTH_STATUS_CHECKING = 0,
    SECC_APP_AUTH_STATUS_OK,
    SECC_APP_AUTH_STATUS_FAIL,
};
//用于向App获取auth成功状态
enum secc_app_auth_status OpenChaoJi_secc_app_check_auth_rfid(struct OpenChaoJi_secc_context * context);
enum secc_app_auth_status OpenChaoJi_secc_app_check_auth_evin(struct OpenChaoJi_secc_context * context, uint8_t evin[17]);

// 车辆充电需求参数
struct OpenChaoJi_secc_gbt27930_charging_requirement_parameter {
    uint16_t voltage;           // VoltageType1, WORD
    uint16_t current;           // CurrentType, WORD
    uint8_t charge_mode;        // enum SystemB_ChargeModeType
};

// 向APP发送当前车辆需求充电电压电流, 要求App根据这个请求计算出实际应当发给充电模块的请求电压和电流
void OpenChaoJi_secc_app_get_power_output(struct OpenChaoJi_secc_context * context
        , const struct OpenChaoJi_secc_gbt27930_charging_requirement_parameter * evcc_requirement
        , unsigned int * volt_power_module, unsigned int * curr_power_module);

#if (OPENCHAOJI_CONFIG_ENABLE_GBT27930_B == CONFIG_YES)

// 向APP请求当前输出能力, APP需要跟上次的能力进行比较
// 如果发生变化了, 需要对 dynamic_output 赋值, 给出当前能力和变化原因, 并返回1
// 如果没有变化, 返回0
int OpenChaoJi_secc_app_check_dynamic_output(struct OpenChaoJi_secc_context * context
        , struct OpenChaoJi_gbt27930_b_msg_charging_ability_secc * dynamic_output);


// 充电结束时请求服务统计信息
void OpenChaoJi_secc_app_get_charge_total_energy(struct OpenChaoJi_secc_context * context
        , OpenChaoJi_gbt27930_b_CapcityType * charge_total_energy);
#endif


/******************************获取充电机设备信息******************************/


/**
 * @brief  获取充电机编号
 * @param  {OpenChaoJi_secc_context *} context 系统上下文指针
 * @return 充电机编号, 1/位, 0偏移量
 */
unsigned int OpenChaoJi_secc_app_get_charger_id(struct OpenChaoJi_secc_context * context);

/**
 * @brief  获取充电机/充电站所在区域编码（数组）
 * @param  {OpenChaoJi_secc_context *} context 系统上下文指针
 * @param  charger_location: 充电机/充电站所在区域编码, 3字节, 标准ASCII码, 低字节在前
 * @return void
 */
void OpenChaoJi_secc_app_get_charger_location(struct OpenChaoJi_secc_context * context, unsigned char *charger_location);

/**
 * @brief  获取充电允许状态标识（充电暂停）
 * @param  {OpenChaoJi_secc_context *} context 系统上下文指针
 * @return 充电允许状态标识, 0x00:充电暂停, 0x01:充电允许
 */
unsigned char OpenChaoJi_secc_app_get_charger_enable(struct OpenChaoJi_secc_context * context);

/**
 * @brief  获取通信超时重连允许次数
 * @param  {OpenChaoJi_secc_context *} context 系统上下文指针
 * @return 允许重连次数, 1次/位, 0次偏移量
 */
unsigned int OpenChaoJi_secc_app_get_permit_reconnect_cnt(struct OpenChaoJi_secc_context * context);

/**
 * @brief  获取充电机累计充电时间（本次）
 * @param  {OpenChaoJi_secc_context *} context 系统上下文指针
 * @return 充电时间, 1min/位, 0min偏移量, 0-600min
 */
unsigned int OpenChaoJi_secc_app_get_charging_time(struct OpenChaoJi_secc_context * context);

/**
 * @brief  获取充电机充电输出能量
 * @param  {OpenChaoJi_secc_context *} context 系统上下文指针
 * @return 充电输出能量, 0.1kWh/位, 0kWh偏移量, 0-1000kWh
 */
unsigned int OpenChaoJi_secc_app_get_output_energy(struct OpenChaoJi_secc_context * context);

#endif
